<?php

namespace Home\Controller;

/**
 * 文档模型控制器
 * 文档模型列表和详情
 */
class ArticleController extends HomeController
{

    /* 文档模型频道页 */
    /* public function index(){
      // 分类信息
      $category = $this->category();

      //频道页只显示模板，默认不读取任何内容
      //内容可以通过模板标签自行定制

      // 模板赋值并渲染模板
      $this->assign('category', $category);
      $this->display($category['template_index']);
      } */

    /* 文档模型列表页 */

    public function index($p = 1)
    { 
        $site_id=C('SITE');
        $name = I('get.name', '');
        $flag = I('get.flag', '');
     
        $tag_id=I('get.id',0);
        $nav=($flag ? 'tags/'.$name : $name);
        
        if (!$name && !$tag_id) {
            $this->error('参数不合法');
        }
        $list_row = C('PAGE_LIST_ROW');
        if($name && $flag!=1){
            /* 分类信息 */
            $category =M('Category')->where(array('name'=>$name))->find();
            $category['icon']=( $category['icon'] ? STATIC_DOMAIN.$category['icon']: $category['icon']);
            $cateOrTags=$category['title'];//用于h5端的分类页显示
            $this->assign('categoryInfo',$category);
        }
        /* 获取当前分类列表 */
        $Document = D('Common/Document');
        $aids=getSetArticle($site_id);//获取到该站点下的所有文章id
        $map = array();
        $map['b.category_id'] = $category['id'];
        $map['a.status'] = 1;
        $map['a.id']=array('in',$aids);
       
        $total = $Document->alias('a')->join('__ARTICLE_CATEGORY__ AS `b` on a.`id` = b.`article_id`')->where($map)->count();
       
        $list = array();
        if ($total > 0) {
            $list = $Document->alias('a')->field('a.*')->page($p, $list_row)->join('__ARTICLE_CATEGORY__ AS `b` on a.`id` = b.`article_id`')->where($map)->select();
            $this->_page($total, $list_row, '/cate/'.$name.'-'.urlencode('[PAGE]').'.html');
        }
        if($flag){
            //标签的id获取到相关文章
            if($tag_id){
                $tagsInfo=M('Tags')->where(array('id'=>$tag_id))->field('id,tag_name')->find();
            }else{
                //获取到标签的id
                $tagsInfo=M('Tags')->where(array('name'=>$name))->field('id,tag_name')->find();
            }
          
           
            $this->assign('title',$tagsInfo['tag_name']);
            $tag_id=$tagsInfo['id'];
            $where['b.site_id']=$site_id;
            $where['a.tags_id']=$tag_id;
            //标签的id获取到相关文章
            $article_id=M('ArticleTags')->alias('a')->join('__ARTICLE_SITE__  AS b  on a.`article_id` = b.`article_id` ')->where($where)->field('a.article_id')->select();
            $article_id=array_column($article_id, 'article_id');
            $cateOrTags=$tagsInfo['tag_name'];//用于h5端的标签页显示
            //dump($article_id);exit;
            if($article_id){
                $where = array();
                $where['id'] =array('in',$article_id);
                $a_id=$article_id;
                $totalDoc =  D('Common/Document')->where($where)->count();
                $list = array();
                if ($totalDoc > 0) {
                    $list =  D('Common/Document')->alias('a')->field('a.*')->page($p, $list_row)->where($where)->select();
                    $this->_page($totalDoc, $list_row, '/tags/'.$name.'-'.urlencode('[PAGE]').'.html');
                }
            } 
            
        }else{
            if($list){
                $article_id=array_column($list, 'id');
            }
            
        }
       
        //dump($list);exit;
        if($list){
            //给当前页面增加封面
            $Document->addCovel($list);
        }
        
        //重新查询数据库的数据
        if ($total > 0) {
            $list = $Document->alias('a')->field('a.*')->page($p, $list_row)->join('__ARTICLE_CATEGORY__ AS `b` on a.`id` = b.`article_id`')->where($map)->select();
            $this->_page($total, $list_row, '/cate/'.$name.'-'.urlencode('[PAGE]').'.html');
        }
        if($flag && $a_id  && $totalDoc){
            $list =  D('Common/Document')->alias('a')->field('a.*')->page($p, $list_row)->where($where)->select();
            $this->_page($totalDoc, $list_row, '/tags/'.$name.'-'.urlencode('[PAGE]').'.html');
        }

        $this->getHomeAd();//右侧广告
        
        $hotDoc=$Document->getSiteNewArticleList($site_id);//获取最热资讯
        $hotDoc=getNameCount($hotDoc,'Document','pin_yin');//对pin_yin属性进行统计

        $this->header();//头部内容
        $this->_listLeftInfo($article_id);//左侧的相关标签
        $this->recommendShop();//推荐店铺

        $categorys =  D('Common/category')->getChildrentitle(1,$article_id);
       
        $header_categorys=implode($categorys, ',');
        C('WEB_SITE_TITLE',$category[title].'_'. C('TITLE'));
        C('WEB_SITE_DESCRIPTION',$header_categorys.'_'. C('TITLE'));
        if($list){
            $list=getNameCount($list,'Document','pin_yin');//统计拼音的个数
        }
        
        $categorys =  D('Common/category')->getSiteCategory(10); 
        $data['nav']=$nav;
        $sumPage=ceil(($totalDoc ? :$total)/$list_row);
        /* 模板赋值并渲染模板 */
        $this->assign('hotDoc',$hotDoc);//热门资讯
        $this->assign('list', $list);
        $this->assign('da',$data);
        $this->assign('sumPage',$sumPage);//总页数
        $this->assign('tags_header',$header_tag);
        $this->assign('category',$categorys);//分类
        $this->assign('cateOrTags',$cateOrTags);//用于h5端的分类或标签页头部的显示
        $this->_display('Article/lists');
    }

    
    public function lists($p = 1)
    {
        $list_row = C('PAGE_LIST_ROW');
        
        $Document =  D('Common/Document');
        
        $total =  D('Common/Document')->getSiteArticleCount(SITE_ID);
        
        $list = array();
        if ($total > 0) {
            $list = $Document->getSiteArticleList(SITE_ID, $p, $list_row);
            $this->_page($total, $list_row, '/lists-'.urlencode('[PAGE]').'.html');
        }
        
        $this->assign('list', $list);
        $this->_display();
    } 
    
    
    /* 文档模型详情页 */

    public function detail($name = '', $p = 1,$id=0,$nav='',$flag=0)
    {
        
        $aid=intval($id);
        /* 标识正确性检测 */
        if (!preg_match('/^[0-9a-z_]+$/', $name)) {
            $this->error('文档ID错误！');
        }
        /* 页码检测 */
        $p = intval($p);
        $p = empty($p) ? 1 : $p;
       
        /* 获取详细信息 */
        $Document =  D('Common/Document');
        $category =  D('Common/Category');

        /* 面包屑 */
        if($flag){
           $data= M('Tags')->where(array('name'=>$nav))->field('tag_name as nav,name')->find();
           $data['url']='tags/'.$nav;  //标签路径
           $param['flag']=intval($flag);
        }else{
           $nav=trim($nav,'.html');
           //dump($nav);exit;
           $data=$category->where(array('name'=>$nav))->field('title as nav,name')->find();
           $data['url']='cate/'.$nav;
        }
        //dump($data);exit;
        $param['name']=$data['name'];
        
        $this->assign('param',$param);//导航中地址栏携带的参数
        
        $this->assign('da',$data);
        
        $info = $Document->detailByname($name,SITE_ID,$aid);
        
        if (!$info) {
            $this->error($Document->getError());
        }
        $info=getNameCount($info,'Document','pin_yin');//统计拼音的个数
        /*关联标签*/
        $association_tags =  D('Common/ArticleCategory')->get_association_tags($info['id'],2);
        
        $category_tag = $category->getChildrentitle(1,$info['id']);
        
        $category_header=implode($category_tag, '.');
        $id = $info['id'];
        //获取到广告
        $ad=M('AdvertiseContent');
        $articleRight=$ad->where('category_id=6')->order('sort')->find();
        //dump($footerAd);exit;
        $tmpl = 'Article/article/detail';

        /* 更新浏览数 */
        $click_rand=mt_rand(1,5);
        $map = array('id' => $id);
        $Document->where($map)->setInc('view',$click_rand);
        
        /* 相关文章 */
        $domain_id = C('SITE');
       
        $relative_article=$Document->relativeArticle($id,$domain_id,3);//文章的相关推荐  
        
        if($relative_article){
            $relative_article=getNameCount($relative_article,'Document','pin_yin');
        }
        
        /* 获取文章的相关标签 */
        $this->_listLeftInfo($id);
        /* 获取前后的文章 */
        $preDoc=$Document->prev($info);
        $nextDoc=$Document->next($info);
        $preDoc=getNameCount($preDoc,'Document','pin_yin');//统计拼音的个数
        $nextDoc=getNameCount($nextDoc,'Document','pin_yin');//统计拼音的个数
        
        $info_header=substr(preg_replace("/[^\x{4e00}-\x{9fa5}]/iu",'',$info['description']),0,255);
        if(!$info_header){
            $info_header=substr($info['description'],0,20);
        }
        $this->_listLeftInfo($id);//左侧的相关标签
        $this->header();//头部内容
        $this->recommendShop();//推荐店铺
        
        C('WEB_SITE_TITLE', $info['title'].'_'.C('TITLE'));
        C('WEB_SITE_KEYWORD', $tags_header);
        C('WEB_SITE_DESCRIPTION', $info_header);
        $this->assign('preDoc',$preDoc);
        $this->assign('nextDoc',$nextDoc);
        $this->assign('info_header',$info_header);
        $this->assign('category_header',$category_header);
        $this->assign('category',$category_tag);//分类

        $this->assign('relative_article',$relative_article);//相关文章
        $this->assign('info', $info);
        $this->assign('page', $p); //页码
        $this->_display($tmpl);
    }

    private function getCategoryByArticleId($article_id)
    {

        /* 获取分类信息 */
        /*$category = D('Category')->info($article_id);
        if ($category && 1 == $category['status']) {
            switch ($category['display']) {
                case 0:
                    $this->error('该分类禁止显示！');
                    break;
                //TODO: 更多分类显示状态判断
                default:
                    return $category;
            }
        } else {
            $this->error('分类不存在或被禁用！');
        }*/
    }

    /* 文档分类检测 */

    private function category($id = 0)
    {
        /* 标识正确性检测 */
        $id = $id ? $id : I('get.category', 0);
        if (empty($id)) {
            $this->error('没有指定文档分类！');
        }
       
        /* 获取分类信息 */
        $category =  D('Common/Category')->info($id);
       
        if ($category && 1 == $category['status']) {
         
            switch ($category['display']) {
                case 0:
                    $this->error('该分类禁止显示！');
                    break;
                //TODO: 更多分类显示状态判断
                default:
                    return $category;
            }
        } else {
            $this->error('分类不存在或被禁用！');
        }
    }
    /* 
     *文章内容图片处理
     * 

    public function dealContent(){
        //查出所有的数据
       $data=M('DocumentArticle')->where()->select();
       
       print_r($data);exit;
       foreach ($data as $v){
           $deal_content['deal_content']=deal_content_img($v['content']);
           M('DocumentArticle')->where(array('id'=>$v['id']))->save($deal_content);
       }
        
    } 
         */

}
